/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef DICTMANAGER_H_
#define DICTMANAGER_H_

#include <vector>
#include <iostream>
#include <map>
#include <set>
using namespace std;

#include <QSettings>
#include <QVariant>
#include <QString>
#include <QVector>
#include <QDir>
#include <QStringList>
#include <QTextStream>
#include <QFile>

#include "SQLiteDB.h"
#include "distance.h"
#include "DictOptions.h"
#include "StaticModule.h"
#include "DictInfo.h"

#define DM INSTANCE(DictManager)


class QueryResult
{
public:
	QueryResult(){}

	QueryResult(QString type, QString dictName, QString key, QString article):
		mDictType(type),
		mDictName(dictName),
		mKey(key),
		mArticle(article)
	{}

	QString getFinalArticle();

public:
	QString mDictName;
	QString mKey;
	QString mArticle;

private:
	QString mDictType;
	QString mFinalArticle;
};

class DictManager
{
public:
	DictManager();
	virtual ~DictManager();

	void readSettings();
	void writeSettings();

	void loadKeyList(DictInfo& dictInfo);

	void setKeyMap(QStringList& queryDictList);

	void loadDicts();
	QStringList getAvailableDictList();

	void lookup(QString key, QStringList& queryDictList);
	QStringList lookupThesaurus(QString key);

	QStringList findKeysBeginWith(QString key, int wordCountMax);
	QStringList findKeysFuzzyWith(QString key, int wordCountMax, int distanceMax);

	void setQueryResult(QString dictName, QString key, bool exactly);
	QString convertXdxf2Html(QString article);

	map<QString, QString>* getExplanationMap(QString dictName);

private:
	SQLiteDB mSQLiteDB;
	QStringList mDictDBs; //database files

	QVector<DictInfo> mDicts; //dictionaries information
	map<QString, QString> mDictMap; //map<dictName, dictDB>

	QVector<QueryResult> mQueryResult;

	map<int, set<QString> > mKeyMap;
	map<QString, map<QString, QString> > mExplanations;

	EditDistance mEditDistance;

	QString xdxf_color_key;
	QString xdxf_color_co;
	QString xdxf_color_ex;
	QString xdxf_color_kref;
	QString xdxf_color_dtrn;
	QString xdxf_color_tense;
};

#endif /* DICTMANAGER_H_ */
